<template>
<div class="login-container">
    <div class="login-content">
      <div class="login-header">
        <img src="../assets/icon/login.png" class="login-logo">
        <h2 class="login-title">图书馆管理系统</h2>
      </div>
      <el-form ref="form" :model="form" :rules="rules" class="login-form">
        <h3 class="form-title">用户登录</h3>
        <el-form-item prop="username">
          <el-input v-model="form.username" placeholder="请输入用户名" clearable>
            <template #prefix>
              <el-icon class="form-icon"><user /></el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model="form.password" placeholder="请输入密码" clearable show-password>
            <template #prefix>
              <el-icon class="form-icon"><lock /></el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item>
          <div class="verify-code-container">
            <el-input v-model="form.validCode" class="verify-code-input" placeholder="请输入验证码"></el-input>
            <ValidCode @input="createValidCode" class="verify-code"/>
          </div>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" class="login-button" @click="login">登 录</el-button>
        </el-form-item>
        <div class="form-footer">
          <el-button type="text" class="register-link" @click="$router.push('/register')">没有账号？立即注册 </el-button>
        </div>
      </el-form>
    </div>
</div>

</template>

<script>
import request from "../utils/request";
import {ElMessage} from "element-plus";
import ValidCode from "../components/Validate";

export default {
  name: "Login",
  components:{
    ValidCode
  },
  data() {
    return {
      validCode: '',//通过valicode获取的验证码
      form: {},
      rules: {
        username: [
          {
            required: true,
            message: '请输入用户名',
            trigger: 'blur',
          }
        ],
        password: [
          {
            required: true,
            message: '请输入密码',
            trigger: 'blur',
          }
        ]

      }

    }
  },
  methods: {
    createValidCode(data){
      this.validCode = data
    },
    login(){
      this.$refs['form'].validate((valid) => {
        if (valid) {
          if (!this.form.validCode) {
            ElMessage.error("请填写验证码")
            return
          }
          if(this.form.validCode.toLowerCase() !== this.validCode.toLowerCase()) {
            ElMessage.error("验证码错误")
            return
          }

          request.post("user/login", this.form).then(res => {
            if (res.code == 0) {
              ElMessage.success("登录成功")
              sessionStorage.setItem("user",JSON.stringify(res.data))//缓存用户信息
              this.$router.push("/dashboard")
            } else {
              ElMessage.error(res.msg)
            }
          })
        }
      })

    }
  }
}

</script>

<style scoped>
.login-container {
  position: fixed;
  width: 100%;
  height: 100vh;
  background: linear-gradient(135deg, var(--primary-color) 0%, var(--secondary-color) 100%);
  display: flex;
  justify-content: center;
  align-items: center;
  overflow: hidden;
}

.login-content {
  width: 420px;
  background: rgba(255, 255, 255, 0.95);
  border-radius: var(--border-radius-large);
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
  overflow: hidden;
  animation: fadeInUp 0.5s;
}

.login-header {
  padding: 30px 0 10px;
  text-align: center;
}

.login-logo {
  width: 70px;
  height: 70px;
}

.login-title {
  color: var(--primary-color);
  font-size: 24px;
  margin-top: 10px;
}

.login-form {
  padding: 20px 40px 40px;
}

.form-title {
  font-size: 20px;
  color: var(--text-primary);
  text-align: center;
  margin-bottom: 25px;
}

.form-icon {
  color: var(--text-secondary);
}

.verify-code-container {
  display: flex;
  gap: 10px;
}

.verify-code-input {
  flex: 1;
}

.verify-code {
  width: 150px;
}

.login-button {
  width: 100%;
  padding: 12px 0;
  font-size: 16px;
  margin-top: 10px;
}

.form-footer {
  margin-top: 15px;
  text-align: center;
}

.register-link {
  color: var(--primary-color);
  font-size: 14px;
}

.register-link:hover {
  text-decoration: underline;
}

@keyframes fadeInUp {
  from {
    opacity: 0;
    transform: translateY(20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}
</style>